 <?
 $sql_grm1 = "select * from tb_question where qID='".$quiz_q."'";
 $rs_grm1 = mysql_query($sql_grm1);
 $data_grm1 = mysql_fetch_object($rs_grm1);

 if (isset($_SESSION['id'])){
 	$sum = 0;
 ?>
 
 <table width="100%" border="0">
   <tr>
     <td style="border-bottom:dotted 1px #333;"> แบบทดสอบ : <?=$data_grm1->qName?> </td>
   </tr>
 </table>
 
 <div class="cleaner_h20"></div>
 
 <table width="100%" bgcolor="#FFFFFF" style="border:dotted 1px #666;">  
    <tr>
      <td>
   

<? if (!isset($submit)) { ?>
		<?
		$xmlFile = "modules/grammar/xmlDoc/".$gramFile.".xml";

		// XML Section
		$data = implode("", file($xmlFile));
		$parser = xml_parser_create();
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
		xml_parse_into_struct($parser, $data, $values, $tags);
		xml_parser_free($parser);

		$questionNo = 1;
		$ch1 = $ch2 = $ch3 = $ch4 = 1;
		$ans = 1;

		foreach ($values as $key => $val) {
			
			// save value to "questions" array if this is a TEXT tag
			if ($val[tag] == "text") {
				$questions[$questionNo]['text'] = $val[value];
				$questionNo++;			
			}
			if ($val[tag] == "ch1") {
				$questions[$ch1]['choices1'] = $val[value];
				$ch1++;	
			}
			if ($val[tag] == "ch2") {
				$questions[$ch2]['choices2'] = $val[value];
				$ch2++;	
			}
			if ($val[tag] == "ch3") {
				$questions[$ch3]['choices3'] = $val[value];
				$ch3++;	
			}
			if ($val[tag] == "ch4") {
				$questions[$ch4]['choices4'] = $val[value];
				$ch4++;	
			}
			if ($val[tag] == "ans") {
				$questions[$ans]['answer'] = $val[value];
				$ans++;			
			}
				
		}
		
		$i_no = 1;

		?>
        <form name="form_quiz" method="post" action="">
		<?
		
		for ($i_no = 1; $i_no <= count($questions); $i_no++){
			
			?>        	
            <table width="100%" style="padding:15px;">
              <tr>
                <td>
                <div> <b><?=$i_no?>.</b>  <?=$questions[$i_no]['text']?></div>
                </td>
              </tr>
              <tr>
                <td><label>
                  <input type="radio" name="RadioGroup[<?=$i_no?>]" value=" <?=$questions[$i_no]['choices1']?>" id="RadioGroup<?=$i_no?>_1" />
                  <?=$questions[$i_no]['choices1']?></label></td>
              </tr>
              <tr>
                <td><label>
                  <input type="radio" name="RadioGroup[<?=$i_no?>]" value=" <?=$questions[$i_no]['choices2']?>" id="RadioGroup<?=$i_no?>_1" />
                  <?=$questions[$i_no]['choices2']?></label></td>
              </tr>
              <tr>
                <td><label>
                  <input type="radio" name="RadioGroup[<?=$i_no?>]" value=" <?=$questions[$i_no]['choices3']?>" id="RadioGroup<?=$i_no?>_1" />
                  <?=$questions[$i_no]['choices3']?></label></td>
              </tr>
              <tr>
                <td style="border-bottom: dotted 1px #666; padding-bottom:15px;"><label>
                  <input type="radio" name="RadioGroup[<?=$i_no?>]" value=" <?=$questions[$i_no]['choices4']?>" id="RadioGroup<?=$i_no?>_1" />
                  <?=$questions[$i_no]['choices4']?></label></td>
              </tr>
        </table>
        	<input type="hidden" id="ans[<?=$i_no?>]" name="ans[<?=$i_no?>]" value="<?=$questions[$i_no]['answer']?>"  />
			<?
			
		}
		
		?>
        <input type="hidden" id="allquest" name="allquest" value="<?=$i_no?>"  /> 
        <input name="submit" type="submit" value="submit" style="margin: 20px;" />
		</form>
        
   <? } else { 
   
   		for ($i = 1; $i < $allquest; $i++){
			//echo $ans[$i]." | ".$RadioGroup[$i]."<br>";
			if (trim($ans[$i]) == trim($RadioGroup[$i])) {
				$sum = $sum +1;
			}
		}
   ?>
   		
        <div style="border:dotted 1px #666; padding: 50px; margin: 20px;" align="center"> 
        คุณได้คะแนนทั้งหมด <?=$sum?> คะแนน 
        </div>
        
     <?
		$sql = "SELECT * FROM tb_quiz WHERE member_id='".$_SESSION['id']."' AND quiz_q = '".$quiz_q."'";
		$rs = mysql_query($sql);
		$check = @mysql_num_rows($rs);
		#echo $sql;
		
		if ($check == 0){
			$sql_scroll = "INSERT INTO tb_quiz VALUES('','".$_SESSION['id']."','".$quiz_q."',".$sum.",'".$today."')";
		} else {
			$sql_scroll = "UPDATE tb_quiz SET quiz_scroll = ".$sum.", datetime = '".$today."' WHERE member_id='".$_SESSION['id']."' AND quiz_q = '".$quiz_q."'";
		}
		
		mysql_query($sql_scroll);
        ?>
   
   	
   <? } ?>
      </td>
    </tr>
    
 </table>

<?
 } else {
?>

  <table width="100%" border="0">
   <tr>
     <td style="border-bottom:dotted 1px #333; padding:50px; margin:20px;" align="center"> กรุณาลงชื่อเข้าสู่ระบบก่อนทำการทดสอบ</td>
   </tr>
  </table> 

<?
 }
?>
 